<template>
    <div class="page">
        <div class="logo">
            <img class="logo-img" src="../assets/userLogo-2.png" alt="" />
            <div class="tab-app" @click="goPage('index')">
                <img src="../assets/119.png" alt="" />
                <span>{{ $t("language.text65") }}</span>
            </div>
            <div class="right-ic" @click="changeLanguage">
                <img src="../assets/100-1.png" alt="" />
                <span>{{ $i18n.locale == "zh" ? "EN" : "中文" }}</span>
                <div class="out" @click="tapOut">{{ $t("language.text76") }}</div>
            </div>
        </div>
        <div class="content">
            <div class="content-tab" v-if="showHotelsTab" @click="showTabPopup = !showTabPopup">
                <img class="ic" src="../assets/tab-ic.png" alt="" />
                {{ $t("language.text73") }}：<span>{{ hotel_name }}</span>
            </div>
            <div class="search-wrap">
                <div class="tab-text" @click="showPopup = !showPopup">
                    <span v-if="userinfo.type != 4">
                        {{
                            is_distribute == 0
                                ? $t("language.text11")
                                : is_distribute == 1
                                ? $t("language.text72")
                                : $t("language.text71")
                        }}
                    </span>
                    <img class="ic" src="../assets/120.png" v-if="userinfo.type != 4" alt="" />
                    <div class="popup" v-if="showPopup">
                        <div class="popup-item" @click.stop="tapPopup(1)" :class="{ select: is_distribute == 1 }">
                            {{ $t("language.text72") }}
                        </div>
                        <div class="popup-item" @click.stop="tapPopup(2)" :class="{ select: is_distribute == 2 }">
                            {{ $t("language.text71") }}
                        </div>
                    </div>
                </div>
                <div class="search">
                    <input type="text" v-model="search" :placeholder="$t('language.search2')" />
                    <img class="ic" src="../assets/101.png" alt="" />
                </div>
            </div>

            <div class="tab-title">
                <!-- <span class="title">{{$i18n.locale == 'zh' ? userinfo.hotel.title : userinfo.hotel.title_en }}</span> -->
                <div class="title">
                    <span v-if="showHotelsTab">{{ hotel_name }}</span>
                </div>
                <div class="tab-btn" @click="tapAssign" v-if="userinfo.type != 4">
                    <img src="../assets/113.png" alt="" />
                    <span v-if="showAssign">{{ $t("language.text31") }}</span>
                    <span v-else>{{ $t("language.text35") }}</span>
                </div>
            </div>
            <!-- @click.self="goPage('details')" -->

            <van-list v-model="loading" :finished="finished" :finished-text="$t('language.noMore')" @load="onLoad">
                <div class="card" v-for="item in list" :key="item.id">
                    <div class="user-name" v-if="item.user && userinfo.type != 4">
                        {{ item.user.name }}
                        <span class="en">{{ item.user.english_name }}</span>
                        <!-- <span class="tag">{{item.user.position}}</span> -->
                    </div>

                    <div class="info-wrap">
                        <div class="title">{{ item.title }}</div>
                        <div class="title small">{{ item.title_alias }}</div>
                        <div class="tag-wrap">
                            <!-- <div class="tag not">待审核</div> -->
                            <!-- <div v-if="item.is_verify == 0" class="tag await">{{$t('language.text12')}}</div>
							<div v-if="item.is_verify == 1" class="tag success">{{$t('language.text13')}}</div>
							<div v-if="item.is_verify == 2" class="tag not">{{$t('language.text14')}}</div> -->
                            <!-- <div class="tag" v-if="item.user_id">{{ $t("language.text71") }}</div> -->
                            <!-- <div class="tag" v-else>{{ $t("language.text72") }}</div> -->
                            <div class="tag" v-if="item.user_id && userinfo.type != 4">{{ $t("language.text71") }}</div>
                            <div class="tag" v-if="!item.user_id && userinfo.type != 4">
                                {{ $t("language.text72") }}
                            </div>
                        </div>
                        <div class="info top-10">
                            {{ $i18n.locale == "zh" ? `${computeDate(item.created_at) + $t("language.text15") }` : `${ $t("language.text15") + computeDate(item.created_at)}` }}:
                            <span class="num">{{ item.room_num || item.be_room_num }}</span>
                        </div>
                        <div class="info top-10">
                            {{ $i18n.locale == "zh" ? `${computeDate(item.created_at) + $t("language.text5") }` : `${ $t("language.text5") + computeDate(item.created_at)}` }}: <span class="num">{{ item.income || item.be_income }}</span>
                        </div>
                        <div class="info top-10">
                            {{ $t("language.text54") }}: <span class="num">{{ item.price_code }}</span>
                        </div>
                    </div>

                    <div class="tap-lnr">
                        <van-switch @click="tapShare(item)" v-model="item.is_share" active-color="#4BD865" />
                        <span>{{ $t("language.text29") }}</span>
                        <template v-if="showAssign">
                            <img
                                @click="tapChecked(item)"
                                v-if="item.checked"
                                class="ic"
                                src="../assets/112.png"
                                alt=""
                            />
                            <img @click="tapChecked(item)" v-else class="ic not" src="../assets/114.png" alt="" />
                        </template>
                    </div>
                </div>
            </van-list>
        </div>

        <div class="select-btn" v-if="showAssign">
            <span>{{ $t("language.text39") }}：{{ total }}</span>
            <div class="roght-btn">
                <div class="btn all-btn" @click="tapAll">{{ $t("language.text84") }}</div>
                <div class="btn" @click="goAssign">{{ $t("language.text38") }}</div>
            </div>
        </div>

        <div class="add-btn">
            <div class="btn select" @click="goPage('customer')">
                <img src="../assets/tab-selcet-2.png" alt="" />
                <span>{{ showHotelsTab ? $t("language.text34") : $t("language.text85") }}</span>
            </div>
            <div class="btn" @click="goPage('sell')">
                <img src="../assets/tab-1.png" alt="" />
                <span>{{ $t("language.text33") }}</span>
            </div>
            <!-- <div class="btn" @click="goPage('assign')">
				<img src="../assets/tab-3.png" alt="" />
				<span>{{$t('language.text35')}}</span>
			</div> -->
            <div class="btn" @click="goPage('check')">
                <img src="../assets/tab-4.png" alt="" />
                <span>{{ $t("language.text36") }}</span>
                <div class="count" v-if="count > 0">
                    <span>{{ count > 9 ? 9 : count }}</span>
                    <span v-if="count > 9" class="add-ic">+</span>
                </div>
            </div>
            <div class="btn" @click="goPage('forms')">
                <img src="../assets/tab-5.png" alt="" />
                <span>{{ $t("language.text37") }}</span>
            </div>
        </div>

        <van-popup v-model="showTabPopup" position="bottom" class="popup-hotels">
            <van-picker
                show-toolbar
                :confirm-button-text="$t('language.text52')"
                :cancel-button-text="$t('language.text53')"
                value-key="hotel_name"
                :default-index="hotel_index"
                :columns="hotels"
                @cancel="showTabPopup = !showTabPopup"
                @confirm="onConfirm"
            />
        </van-popup>
    </div>
</template>
<script>
import { removeToken } from "@/utils/auth";
import { getHotelLnr, setLnrShare, verifyCount, getUserInfo, beHotelLnrLists } from "@/api/user";
import { Toast } from "vant";
let timer = null;

export default {
    name: "Customer",
    data: function () {
        return {
            locale: "zh",
            search: "",
            hotel_name: "",
            userinfo: {
                type: 2,
                hotel: {},
            },
            checked: false,
            showAssign: false,
            list: [],
            total: 0,
            loading: false,
            showPopup: false,
            showTabPopup: false,
            finished: false,
            page: 1,
            is_distribute: 0, // 1分配过 0未分配
            count: 0,
            hotels: [],

            hotel_id: "",
            hotel_code: "",
            hotel_index: 0,
            showHotelsTab: false,
        };
    },
    async created() {
        let _hotels = localStorage.getItem("hotels");
        if (_hotels) {
            _hotels = JSON.parse(_hotels);
            this.hotel_name = _hotels.hotel_name;
        }

        this.getUserInfoData();
        this.getVerifyCount();
    },
    watch: {
        search: function () {
            const t = this;
            if (timer != null) clearTimeout(timer);

            timer = setTimeout(() => {
                timer = null;
                clearTimeout(timer);
                t.page = 1;
                t.list = [];
                t.getHotelLnrData();
            }, 500);
        },
    },
    mounted() {},
    methods: {
		computeDate(str) {
			let date = new Date(str);
			date.setMonth(date.getMonth() - 1);
			return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}`;
		},
        tapOut() {
            removeToken();
            localStorage.removeItem("userinfo");
            localStorage.removeItem("hotels");
            this.$router.replace(`/login`);
        },
        onConfirm(value, index) {
            const { hotel_id, hotel_name, hotel_code } = value;
            this.page = 1;
            this.list = [];
            value.hotel_index = index;
            this.hotel_id = hotel_id;
            this.hotel_name = hotel_name;
            this.hotel_code = hotel_code;
            this.hotel_index = index;
            this.showTabPopup = false;
            localStorage.setItem("hotels", JSON.stringify(value));
            this.getHotelLnrData();
        },
        tapAll() {
            this.list = this.list.map((item) => {
                item.checked = true;
                return item;
            });
            let data = [];
            const list = this.list;
            if (list.length > 0) {
                list.forEach((item) => {
                    if (item.checked) {
                        data.push(item.id);
                    }
                });
            }
            this.total = data.length;
        },
        tapAssign() {
            this.showAssign = !this.showAssign;
        },
        tapPopup(item) {
            this.is_distribute = item;
            this.page = 1;
            this.list = [];
            this.showPopup = false;
            this.getHotelLnrData();
        },
        tapShare(item) {
            let isShow = 0;
            if (!item.is_share) {
                isShow = 1;
            }
            setLnrShare({
                is_share: isShow,
                lnr_id: item.id,
            })
                .then((res) => {
                    if (res.status_code != 200) {
                        Toast(res.message);
                    } else {
                        Toast(this.$i18n.tc("language.text66"));
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        tapChecked(item) {
            if (item.checked) {
                item.checked = false;
            } else {
                item.checked = true;
            }
            this.$forceUpdate();
            let data = [];
            const list = this.list;
            if (list.length > 0) {
                list.forEach((item) => {
                    if (item.checked) {
                        data.push(item.id);
                    }
                });
            }
            this.total = data.length;
        },
        goAssign() {
            const list = this.list;

            let data = [];
            if (list.length > 0) {
                list.forEach((item) => {
                    if (item.checked) {
                        data.push(item.id);
                    }
                });
            }
            if (data.length == 0) return;
            this.$router.push({
                path: "selectSell",
                query: {
                    data,
                },
            });
        },
        onLoad() {
            this.getHotelLnrData();
        },
        async getHotelLnrData() {
            const userinfo = localStorage.getItem("userinfo");
            const { type } = JSON.parse(userinfo);
            if (type !== 4) {
                getHotelLnr({
                    page: this.page,
                    is_distribute: this.is_distribute,
                    key: this.search,
                    is_verify: 1,
                })
                    .then((res) => {
                        let { current_page, last_page, data } = res.data;
                        data.forEach((item) => {
                            item.is_share === 0 ? (item.is_share = false) : (item.is_share = true);
                        });
                        // 加载状态结束
                        this.loading = false;
                        // 数据全部加载完成
                        if (current_page == last_page) {
                            this.finished = true;
                        } else {
                            this.finished = false;
                            this.page++;
                        }
                        this.list = [...this.list, ...data];
                    })
                    .catch((err) => {
                        this.loading = false;
                        console.log(err);
                    });
                return;
            }
            beHotelLnrLists({
                page: this.page,
                is_distribute: this.is_distribute,
                key: this.search,
                // is_verify: 1
            })
                .then((res) => {
                    let { current_page, last_page, data } = res.data;
                    data.forEach((item) => {
                        item.is_share === 0 ? (item.is_share = false) : (item.is_share = true);
                    });
                    // 加载状态结束
                    this.loading = false;
                    // 数据全部加载完成
                    if (current_page == last_page) {
                        this.finished = true;
                    } else {
                        this.finished = false;
                        this.page++;
                    }
                    this.list = [...this.list, ...data];
                })
                .catch((err) => {
                    this.loading = false;
                    console.log(err);
                });
        },
        getVerifyCount() {
            verifyCount()
                .then((res) => {
                    this.count = res.data;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        getUserInfoData() {
            getUserInfo()
                .then((res) => {
                    const { type, hotels } = res.data;
                    this.userinfo = res.data;
                    this.showHotelsTab = type !== 4 ? true : false;
                    this.hotels = hotels;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        goPage(page, id) {
            this.$router.push({
                path: page,
                query: {
                    id,
                },
            });
        },
        changeLanguage() {
            if (this.$i18n.locale == "zh") {
                this.$i18n.locale = "en";
                this.$cookies.set("locale", "en");
            } else {
                this.$i18n.locale = "zh";
                this.$cookies.set("locale", "zh");
            }
        },
    },
};
</script>
<style type="text/css" lang="scss" scoped>
.page {
    background-color: #fff;
}

.logo {
    background-color: #fff;

    .tab-app {
        display: flex;
        align-items: center;
        font-size: 24px;
        color: #333333;

        img {
            width: 31px;
            height: 30px;
            margin-right: 6px;
        }
    }

    .right-ic {
        color: #333333;
    }

    .out {
        font-size: 24px;
        color: #333333;
        margin-left: 24px;
    }
}

.content {
    padding-bottom: 118px;
    border-top: 8px solid #f2f5f7;

    .content-tab {
        display: flex;
        align-items: center;
        margin: 0 30px;
        padding-top: 30px;
        font-size: 30px;

        .ic {
            width: 32px;
            height: 33px;
            margin-right: 12px;
        }
    }

    .search-wrap {
        padding-top: 40px;
        padding-bottom: 16px;
        margin: 0 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;

        .tab-text {
            font-size: 30px;
            color: #666666;
            position: relative;

            .ic {
                width: 14px;
                height: 10px;
                margin-left: 12px;
                position: relative;
                top: -4px;
            }

            .popup {
                position: absolute;
                top: 54px;
                left: 0;
                padding: 0 24px;
                min-width: 200px;
                background-color: #fff;
                z-index: 100;
                border-radius: 10px;
                box-shadow: 0 8px 18px #cccccc;

                .popup-item {
                    white-space: nowrap;
                    padding: 24px 0;
                    font-size: 26px;
                    text-align: center;
                    border-bottom: 1px solid #eeeeee;

                    &.select {
                        color: #e8542c;
                    }

                    &:last-child {
                        border-bottom: none;
                    }
                }
            }
        }

        .search {
            position: relative;
            z-index: 10;
            padding: 0 28px;
            width: 68%;
            height: 74px;
            margin-left: 24px;
            border: 2px solid #1f4456;
            background-color: #fff;
            box-sizing: border-box;
            border-radius: 45px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            input {
                font-size: 24px;
                color: #666;
                width: 100%;
                height: 100%;
            }

            .ic {
                width: 28px;
                height: 28px;
                flex-shrink: 0;
            }
        }
    }

    .tab-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 0 30px;

        .title {
            font-size: 36px;
            color: #1f4456;
            font-weight: bold;
        }

        .tab-btn {
            font-size: 28px;
            color: #e8542c;
            display: flex;
            align-items: center;

            img {
                width: 32px;
                height: 32px;
                margin-right: 8px;
            }
        }
    }

    .card {
        margin: 24px 30px 0;
        padding: 30px;
        box-sizing: border-box;
        border-radius: 10px;
        background-color: #fff;
        box-shadow: 0 0 24px #eee;
        position: relative;

        .title {
            font-weight: bold;
            font-size: 36px;
            font-weight: bold;
            color: #333333;

            &.small {
                font-size: 24px;
                margin-top: 8px;
            }
        }

        .tag-wrap {
            display: flex;
            flex-wrap: wrap;
            padding: 10px 0;

            .tag {
                padding: 8px 18px;
                border-radius: 0 8px 8px 8px;
                color: #e8542c;
                font-size: 22px;
                margin-right: 12px;
                background-color: #fbe8e2;

                &.not {
                    color: #fff;
                    background-color: #f01a1a;
                }
            }
        }

        .user-name {
            font-size: 30px;
            color: #333333;
            font-weight: bold;

            .tag {
                position: relative;
                top: 4px;
                color: #1f4456;
                margin-left: 12px;
                display: inline-block;
                max-width: 300px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                padding: 8px 12px;
                border-radius: 18px 18px 18px 0;
                font-size: 20px;
                background-color: #edf2f5;
            }

            .en {
                margin-left: 10px;
            }
        }

        .info-wrap {
            margin-top: 24px;
            position: relative;

            .info {
                font-size: 24px;
                color: #666666;

                .num {
                    color: #1f4456;
                }

                &.top-10 {
                    margin-top: 10px;
                }
            }
        }

        .tap-lnr {
            display: flex;
            align-items: center;
            color: #999999;
            font-size: 24px;
            padding-top: 40px;
            margin-top: 24px;
            position: relative;

            span {
                padding-left: 12px;
            }

            .ic {
                width: 54px;
                height: 54px;
                position: absolute;
                top: 84%;
                right: 0;
                transform: translateY(-50%);

                &.not {
                    width: 38px;
                    height: 38px;
                }
            }
        }
    }
}

.select-btn {
    position: fixed;
    left: 50%;
    bottom: 114px;
    transform: translateX(-50%);
    z-index: 10001;
    width: 100%;
    height: 114px;
    max-width: 750px !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 114px;
    background-color: rgba(31, 68, 86, 0.8);
    font-size: 28px;
    color: #fff;
    padding: 0 30px;
    box-sizing: border-box;

    .roght-btn {
        display: flex;
        align-items: center;
    }

    .btn {
        padding: 12px 20px;
        background-color: #e8542c;
        border-radius: 10px;
    }
    .all-btn {
        margin-right: 16px;
        background-color: #1f4456;
    }
}

.add-btn {
    position: fixed;
    left: 50%;
    bottom: 0;
    z-index: 10000;
    transform: translateX(-50%);
    width: 100%;
    height: 114px;
    max-width: 750px !important;
    color: #fff;
    font-size: 28px;
    background-color: #fff;
    box-shadow: 0 2px 24px #eee;
    padding: 14px 30px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    text-align: center;

    .btn {
        color: #999999;
        font-size: 24px;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        max-width: 18%;
        margin: 0 12px;
        height: 100%;
        position: relative;

        &.select {
            color: #1f4456;
        }

        .count {
            position: absolute;
            top: -10px;
            right: 0;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            line-height: 44px;
            text-align: center;
            font-size: 22px;
            color: #ffffff;
            background-color: #e8542c;

            .add-ic {
                font-size: 20px;
                position: relative;
                top: -8px;
            }
        }

        img {
            width: 38px;
            height: 38px;
            margin-bottom: 8px;
            object-fit: contain;
        }
    }
}

.van-dialog {
    width: 80%;
    padding: 80px 58px;
    box-sizing: border-box;

    .van-dialog__header {
        font-size: 36px;
        color: #333333;
        font-weight: bold;
        margin-bottom: 30px;
    }

    .van-dialog__content {
        font-size: 22px;
        color: #666666;
        margin-bottom: 34px;

        .van-dialog__message {
            line-height: 34px;
        }
    }

    .van-goods-action-button--danger {
        width: 80%;
        height: 74px;
        font-size: 24px;
        color: #fff;
        background: #1f4456 !important;
        border-radius: 10px;
    }
}

.van-popup.popup-hotels {
    z-index: 100000 !important;
}
</style>
